﻿using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace BLL
{
    /// <summary>
    /// 图书借还业务层
    /// </summary>
   public class BorrowReturnMBLL
    {
        private Dictionary<string, string> keyValuePairs;

        BorrowReturnMDAL borrowReturnMDAL = new BorrowReturnMDAL();


        /// <summary>
        /// 根据读者编号查询读者信息
        /// </summary>
        /// <param name="ReaderId">读者编号</param>
        /// <returns></returns>
        public Dictionary<string, string> getReturn(string ReaderId)
        {
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(ReaderId))
            {
                DataTable dataTable = borrowReturnMDAL.getReturnDAL(ReaderId);
                if (dataTable.Rows.Count>0)
                {
                    keyValues.Add(EnumTools.GetDescription(ManageEnums.READER.READER_ID), dataTable.Rows[0][1].ToString());
                    keyValues.Add(EnumTools.GetDescription(ManageEnums.READER.READER_NAME), dataTable.Rows[0][2].ToString());
                }
            }
            keyValuePairs = keyValues;
            return keyValues;
        }

        /// <summary>
        /// 根据图书id及搜索图书
        /// </summary>
        /// <param name="BookID">图书id</param>
        /// <returns></returns>
        public DataTable getBookList(string BookID)
        {
            return borrowReturnMDAL.getBookDAL(BookID);
        }

        /// <summary>
        /// 还书列表
        /// </summary>
        /// <param name="Reader">读者id</param>
        /// <returns></returns>
        public DataTable getBorrowReturnList(string Reader)
        {
           return borrowReturnMDAL.getBorrowReturnDAL(Reader);
        }

        /// <summary>
        /// 借书
        /// </summary>
        /// <param name="BookID">图书id</param>
        /// <returns></returns>
        public string AddBorrow(string BookID)
        {
            if (keyValuePairs!=null)
            {
               bool BR= borrowReturnMDAL.geAddBorrowDAL(BookID, keyValuePairs[EnumTools.GetDescription(ManageEnums.READER.READER_ID)]);
                if (BR)
                {
                    return EnumTools.GetDescription(ManageEnums.BORROW.SUCCEED);
                }
                else
                {
                    return EnumTools.GetDescription(ManageEnums.BORROW.DEFEATED);
                }
            }
            else
            {
                return "请输入读者编号!";
            }
        }

        /// <summary>
        /// 还书
        /// </summary>
        /// <param name="BookID"></param>
        /// <param name="BorrowReturnID"></param>
        /// <returns></returns>
        public bool ReturnBook(string BookID,string BorrowReturnID)
        {
                return borrowReturnMDAL.ReturnBookDAL(BookID, BorrowReturnID);
        }
    }
}
